package com.yugu.util;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import org.springframework.beans.BeanUtils;
import com.github.pagehelper.util.StringUtil;
public class BeanCopyUtil {
	public static void copyProperties(Object source, Object target, List<String> replaceChars) {
        BeanUtils.copyProperties(source, target);
        if (replaceChars != null && !replaceChars.isEmpty()) {
            PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(source.getClass());
            PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
            for (PropertyDescriptor sourcePd : sourcePds) {
                if (sourcePd.getReadMethod() != null) {
                    for (PropertyDescriptor targetPd : targetPds) {
                        if (targetPd.getWriteMethod() != null && sourcePd.getName().equals(targetPd.getName())) {
                            try {
                                Object sourceValue = sourcePd.getReadMethod().invoke(source);
                                if (sourceValue != null && sourceValue instanceof String) {
                                    String targetValue = (String) sourceValue;
                                    for (String replaceChar : replaceChars) {
                                        targetValue = targetValue.replace(replaceChar, "");
                                    }
                                    targetPd.getWriteMethod().invoke(target, targetValue);
                                } else {
                                    targetPd.getWriteMethod().invoke(target, sourceValue);
                                }
                            } catch (IllegalAccessException | IllegalArgumentException
                                    | InvocationTargetException e) {
                                e.printStackTrace();
                            }
                            break;
                        }
                    }
                }
            }
        }
    }
	public static void copyProperties(Object source, Object target) {
        	BeanUtils.copyProperties(source, target);
            PropertyDescriptor[] sourcePds = BeanUtils.getPropertyDescriptors(source.getClass());
            PropertyDescriptor[] targetPds = BeanUtils.getPropertyDescriptors(target.getClass());
            for (PropertyDescriptor sourcePd : sourcePds) {
                if (sourcePd.getReadMethod() != null) {
                    for (PropertyDescriptor targetPd : targetPds) {
                        if (targetPd.getWriteMethod() != null && sourcePd.getName().equals(targetPd.getName())) {
                            try {
                                Object sourceValue = sourcePd.getReadMethod().invoke(source);
                                if (sourceValue != null && sourceValue instanceof String) {
                                    String targetValue = (String) sourceValue;
                                    if(!StringUtil.isEmpty(targetValue))
                                    targetPd.getWriteMethod().invoke(target, targetValue);
                                } else {
                                    targetPd.getWriteMethod().invoke(target, sourceValue);
                                }
                            } catch (IllegalAccessException | IllegalArgumentException
                                    | InvocationTargetException e) {
                                e.printStackTrace();
                            }
                            break;
                        }
                    }
                }
            }
    }
	/**
     * 复制bean属性
     * 
     * @param source 源bean
     * @param targetClass 目标bean的class
     * @param replaceChars 需要替换的字符列表
     * @return 目标bean
     */
    public static <T> T copyProperties(Object source, Class<T> targetClass, List<String> replaceChars) {
        T target = null;
        try {
            target = targetClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        copyProperties(source, target);
        return target;
    }
}
